<?php

namespace app\controller;

use app\BaseController;
use think\captcha\facade\Captcha;
use think\facade\Db;
use think\facade\Session;

class User extends BaseController
{
    //获取登录记录
    public function getLoginLog()
    {
        $data = input('post.');
        $user = Session::get('user');
        if ($user['type'] == 1) {
            $log = Db::table('tb_login_log')->where('uid', $user['uid'])->select();
        } else {
            $log = Db::table('tb_login_log')->select();
        }
        return (returnJson(data: $log));
    }
    //获取进服记录
    public function getPlayRecord()
    {
        $user = checkLogin();
        $log = Db::table('tb_record_play')->where('uid', $user['uid'])->select();
        return (returnJson(data: $log));
    }
    //获取进服记录 For Server.php
    public function getPlayRecordForServer()
    {
        $user = checkLogin();

        $log = Db::table('tb_record_play')
            ->alias('a')
            ->join('tb_server b', 'a.sid = b.id')
            ->where('b.uid', $user['uid'])
            ->select();
        return (returnJson(data: $log));
    }
    // 获取自己的服务器列表
    public function getMyServerList()
    {
        $user = checkLogin();
        $server = Db::table('tb_server')->where('uid', $user['uid'])->select();
        return (returnJson(data: $server));

    }
    // 获取用户信息[详细]
    public function getUserInfo()
    {
        $user = checkLogin();
        $userInfo = Db::table('tb_user')->where('id', $user['uid'])
            ->field('id,nickname,create_time,update_time,phone,email,points,schub')
            ->find();
        if ($userInfo['phone'] != null) {
            $userInfo['phone'] = 1;
        } else {
            $userInfo['phone'] = 0;
        }
        if ($userInfo['email'] != null) {
            $userInfo['email'] = 1;
        } else {
            $userInfo['email'] = 0;
        }
        if ($userInfo['schub'] != null) {
            $userInfo['schub'] = 1;
        } else {
            $userInfo['schub'] = 0;
        }
        return (returnJson(data: $userInfo));
    }
    // 修改密码
    public function changePassword()
    {
        $user = checkLogin();
        $data = input('post.');
        $userInfo = Db::table('tb_user')->where('id', $user['uid'])->find();
        if ($userInfo['password'] != hash("sha256", "scnet_".$data['oldPassword'])) {
            return (returnJson(1, msg: "原密码错误"));
        }
        $res = Db::table('tb_user')->where('id', $user['uid'])->update(['password' => hash("sha256", "scnet_".$data['newPassword'])]);
        if ($res) {
            return (returnJson(msg: "修改成功"));
        } else {
            return (returnJson(1, msg: "修改失败"));
        }
    }
    // 修改头像
    public function changeAvatar()
    {
        $user = checkLogin();
        //获取上传的文件
        $file = request()->file();
        if (!$file) {
            return (returnJson(1, msg: "请上传文件"));
        }
       $url=uploadS3($file['file']);
        if (!$url) {
            return (returnJson(1, msg: "上传失败"));
        }
        $res = Db::table('tb_user')->where('id', $user['uid'])->update(['avatar' => $url]);
        if ($res) {
            return (returnJson(msg: "修改成功"));
        } else {
            return (returnJson(1, msg: "修改失败"));
        }
    }
}